import { useMutation, useQuery } from "react-query";
import { useRequest } from "../utils/request";
import { IPage } from "../types";

/**
 * !获取街道接口
 */
export const useGetJdUnit = (queryParams: {
  page: number;
  limit: number;
  jdUnitName?: string;
}) => {
  const request = useRequest<IPage<JdUnit>>();
  return useQuery(["getJdUnitBySearch", queryParams], () =>
    request("/jdUnit/getJdUnitBySearch", { data: queryParams })
  );
};

/**
 * !添加街道接口
 */
export const useAddJdUnit = () => {
  const request = useRequest<string>();
  return useMutation(async (params: { jdUnitName: string }) => {
    const result = request("/jdUnit/addJdUnit", {
      data: params,
      method: "PUT",
    });
    return result;
  });
};

/**
 * !修改街道接口
 */
export const useUpdateJdUnit = () => {
  const request = useRequest<string>();
  return useMutation(
    async (params: { jdUnitId: string; jdUnitName: string }) => {
      const result = request("/jdUnit/updateJdUnit", {
        data: params,
        method: "POST",
      });
      return result;
    }
  );
};

/**
 * !删除街道接口
 */
export const deleteJdUnitById = () => {
  const request = useRequest<string>();
  return useMutation(async (params: { jdUnitId: string }) => {
    const result = request("/jdUnit/deleteJdUnitById", {
      data: params,
      method: "DELETE",
    });
    return result;
  });
};

export type JdUnit = {
  /**
   * 街道单位id
   */
  jdUnitId: string;

  /**
   * 街道name
   */
  jdUnitName: string;
};
